package com.yikaiye.android.yikaiye.ui.activity.order;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;

import com.bumptech.glide.Glide;
import com.yikaiye.android.yikaiye.R;
import com.yikaiye.android.yikaiye.base.BaseActivity;
import com.yikaiye.android.yikaiye.config.Constant;
import com.yikaiye.android.yikaiye.databinding.ActivityLookOrderProgressBinding;
import com.yikaiye.android.yikaiye.di.retrofit.ApiConstants;
import com.yikaiye.android.yikaiye.model.EventBusModel;
import com.yikaiye.android.yikaiye.model.OrderProgressModel;
import com.yikaiye.android.yikaiye.ui.activity.mine.CompanyDetailsActivity;
import com.yikaiye.android.yikaiye.ui.activity.order.viewmodel.OrderProgressViewModel;
import com.yikaiye.android.yikaiye.ui.activity.service.ServiceDetailsActivity;
import com.yikaiye.android.yikaiye.ui.adapter.OrderProgressAdapter;
import com.yikaiye.android.yikaiye.utils.CommonUtil;
import com.yikaiye.android.yikaiye.utils.JumpIntent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

/**
 * 订单查看进度
 */
public class LookOrderProgressActivity extends BaseActivity<ActivityLookOrderProgressBinding, OrderProgressViewModel> {

    private OrderProgressAdapter adapter;
    private String creditNo;
    /**
     * order:从订单过来，serviceDetails从服务详情过来
     */
    private String from;

    @Override
    protected OrderProgressViewModel bindModel() {
        return getViewModel(this, OrderProgressViewModel.class);
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
        adapter = new OrderProgressAdapter(context);
        binding.rv.setAdapter(adapter);
        adapter.setEmptyViewEnable(true);
        adapter.setEmptyViewLayout(context, R.layout.layout_empty_list);
    }

    @Override
    protected void initData() {
        initTitleBar(getString(R.string.look_progress));
        if (getIntent().getExtras() != null) {
            String id = getIntent().getExtras().getString(Constant.ID_KEY);
            from = getIntent().getExtras().getString(Constant.FROM_KEY);
            viewModel.getServiceByOrderId(id);
        }
    }

    @Override
    protected void initClick() {
        viewModel.progressDataMutableLiveData.observe(this, orderProgressModels -> {
            adapter.submitList(orderProgressModels);
            if (orderProgressModels != null && !orderProgressModels.isEmpty()) {
                OrderProgressModel model = orderProgressModels.get(0);
                if (model != null) {
                    creditNo = model.getCreditNo();
                    binding.productNameTv.setText(CommonUtil.checkText(model.getProductName()));
                    binding.productDesTv.setText(CommonUtil.checkText(model.getSlogan()));
                    Glide.with(context).load(ApiConstants.BASE_IMAGE_RELEASE_URL + model.getAppBanner()).placeholder(R.mipmap.default_image).into(binding.productLogoIv);
                    if (TextUtils.isEmpty(model.getCompanyName())) {
                        binding.line.setVisibility(View.GONE);
                        binding.companyLl.setVisibility(View.GONE);
                    }
                }
            }
        });

        binding.companyLl.setOnClickListener(view -> {
            Bundle bundle = new Bundle();
            bundle.putString(Constant.CREDIT_NO_KEY, creditNo);
            JumpIntent.jump((Activity) context, CompanyDetailsActivity.class, bundle);
        });

        adapter.setOnItemClickListener((baseQuickAdapter, view, i) -> {
            if (baseQuickAdapter.getItem(i) != null) {
                if (from.equals(Constant.service)) {
                    finish();
                    EventBus.getDefault().post(new EventBusModel(Constant.finishOrderDetailsPage));
                }  else {
                    Bundle bundle = new Bundle();
                    bundle.putString(Constant.ID_KEY, baseQuickAdapter.getItem(i).getId());
                    bundle.putString(Constant.FROM_KEY, from);
                    JumpIntent.jump((Activity) context, ServiceDetailsActivity.class, bundle);
                }
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void Event(EventBusModel eventBusModel) {
        if (null != eventBusModel) {
            if (Constant.finishLookProgressDetailsPage.equals(eventBusModel.getMessage())) {
                finish();
            }
        }
    }
}